/*
 * AcceleratedObject.cpp
 *
 *  Created on: 30/05/2013
 *      Author: vitor
 */

#include "AcceleratedObject.h"

AcceleratedObject::AcceleratedObject(Animation* animation, Shape* shape,
		int hitPoints) :
		GameObject(shape) {
	_animation = animation;
	_hitPoints = hitPoints;
	_speed = _acceleration = _rotation = 0;
}

void AcceleratedObject::render(float cameraX, float cameraY) {
	_animation->render(_shape->getCenterRef()->getX() - cameraX,
			_shape->getCenterRef()->getY() - cameraY, true);
}

int AcceleratedObject::update(double dt) {

	_acceleration = 0;
	_animation->setFrameTime(200);

	if (InputManager::getInstance()->isKeyPressed(SDLK_d)) {
		_rotation -= 80 * dt;
	}
	if (InputManager::getInstance()->isKeyPressed(SDLK_a)) {
		_rotation += 80 * dt;
	}
	if (InputManager::getInstance()->isKeyPressed(SDLK_w)) {
		_acceleration = 80;
		_animation->setFrameTime(6);
	}
	if (InputManager::getInstance()->isKeyPressed(SDLK_s))
		_acceleration = -80;

	_speed += _acceleration * dt;

	_vecAccel.setX(-cos((_rotation - 90) * M_PI / 180));
	_vecAccel.setY(sin((_rotation - 90) * M_PI / 180));

	_vecSpeed.setX(-cos((_rotation - 90) * M_PI / 180));
	_vecSpeed.setY(sin((_rotation - 90) * M_PI / 180));

	_vecAccel = _vecAccel.constantProduct(_acceleration);
	_vecSpeed = _vecSpeed.constantProduct(_speed);

	_shape->getCenterRef()->setX(
			_shape->getCenterRef()->getX() + _vecSpeed.getX() * dt);
	_shape->getCenterRef()->setY(
			_shape->getCenterRef()->getY() + _vecSpeed.getY() * dt);

	_animation->update(dt);
	_animation->rotoZoom(_rotation, 1, 1, false);

	return 0;
}

AcceleratedObject::~AcceleratedObject() {
	// TODO Auto-generated destructor stub
}

